package com.groupbuying.store.controller.forInternal;

import com.groupbuying.store.connector.SysconfigFeign;
import com.groupbuying.store.constants.CommonConstants;
import com.groupbuying.store.context.FilterContextHandler;
import com.groupbuying.store.controller.forManage.BaseController;
import com.groupbuying.store.domain.StoreAccountDO;
import com.groupbuying.store.domain.forManage.StoreRoleDO;
import com.groupbuying.store.dto.LoginUserDTO;
import com.groupbuying.store.dto.forManage.AccountDTO;
import com.groupbuying.store.dto.forManage.do2dto.AccountConvert;
import com.groupbuying.store.service.StoreAccountService;
import com.groupbuying.store.service.StoreAccountShopRelService;
import com.groupbuying.store.service.forManage.StoreRoleService;
import com.groupbuying.store.utils.PageUtils;
import com.groupbuying.store.utils.Query;
import com.groupbuying.store.utils.R;
import com.groupbuying.store.utils.apiresult.ApiResult;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api("服务商用户中心")
@RequestMapping("/user")
@RestController
public class StoreAccountController extends BaseController {
    @Autowired
	StoreAccountService storeAccountService;

    @Autowired
	StoreRoleService roleService;

	@Autowired
	SysconfigFeign sysconfigFeign;

	@Autowired
	StoreAccountShopRelService storeAccountShopRelService;

	private static final Logger LOGGER = LoggerFactory.getLogger(StoreAccountController.class);

	/**
	 * 服务商登录获取用户信息
	 * @returncurrentUser
	 */
    @GetMapping("/currentUser")
	public LoginUserDTO currentUser(){
		LoginUserDTO loginUserDTO = new LoginUserDTO();
    	BigInteger roleId = roleService.roleIdsByUserId(FilterContextHandler.getUserID()).get(0);
		StoreRoleDO roleDO = roleService.get(roleId);
		//StoreAccountDO storeAccountDO = storeAccountService.get(FilterContextHandler.getUserID());
		loginUserDTO.setProviderId(FilterContextHandler.getName());

		//通过服务商id获取服务商主账号
		Map searchMap = new HashMap();
		searchMap.put("providerId",FilterContextHandler.getName());
		searchMap.put("providerType","0");
		List<StoreAccountDO> storeAccountDOMainList = storeAccountService.list(searchMap);
		StoreAccountDO storeAccountDOMain = storeAccountDOMainList.get(0);
		loginUserDTO.setName("(" + roleDO.getRoleName() + ")" + storeAccountDOMain.getProviderName());
		loginUserDTO.setUsername(storeAccountDOMain.getProviderName());
		return loginUserDTO;
	}

	/**
	 * 根据用户id获取用户currentUser
	 * @param id
	 * @return
	 */
    @GetMapping("{id}")
	R get(@PathVariable("id") String id ){
		StoreAccountDO storeAccountDO = storeAccountService.get(id);
		AccountDTO accountDTO = AccountConvert.MAPPER.do2dto(storeAccountService.get(id));
    	return R.ok().put("data",accountDTO);
	}

	/**
	 * 分页获取用户列表
	 * @param params
	 * @return
	 */
    @GetMapping()
    public R listByPage(@RequestParam Map<String, Object> params) {
    	params.put("delFlag",CommonConstants.COMMON_FLAG_UNDEL);
		String providerId = FilterContextHandler.getName();
		params.put("providerId",providerId);
        Query query = new Query(params);
        List<AccountDTO> userDTOS = AccountConvert.MAPPER.dos2dtos((storeAccountService.list(query)));
        int total = storeAccountService.count(query);
        PageUtils pageUtil = new PageUtils(userDTOS, total);
		return R.ok().put("page",pageUtil);
    }

	/**
	 * 修改用户密码
	 * @param zmlmagic
	 * @return
	 */
	@PostMapping("/changepwd")
	public ApiResult changepwd(@RequestBody Map<String, Object> param) {
		return storeAccountService.changeUserPassword(param);
	}


	/**
	 * 删除用户
	 * @param id
	 * @return
	 */
	@DeleteMapping()
	R remove( String id) {
		return R.operate (storeAccountService.remove(id) > 0);
	}

	@PostMapping("/batchRemove")
	@ResponseBody
	R batchRemove(@RequestParam("ids[]") String[] userIds) {
		int r = storeAccountService.batchRemove(userIds);
		if (r > 0) {
			return R.ok();
		}
		return R.error();
	}

	@PostMapping("/exits")
	@ResponseBody
	boolean exits(@RequestParam Map<String, Object> params) {
		// 存在，不通过，false
		return !storeAccountService.exits(params);
	}

	//获取首页流水登陆信息以及余额
	@PostMapping("/getUshop")
	public ApiResult getUshop(){
		return  storeAccountService.getUshop();
	}

	//获取流水表

	@PostMapping("/getFlowStore")
	public ApiResult getFlowStore(@RequestParam Map<String,String> map){
			return  storeAccountService.getFlowStore(map);
	}
}
